package com.example.understand_compose1.fet_viewpager2.presentation

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.viewpager2.widget.ViewPager2
import com.example.understand_compose1.R
import com.example.understand_compose1.databinding.ActivityFetViewPager2Binding
import com.example.understand_compose1.fet_viewpager2.adapter.VPGAdapter

class FetViewPager2Activity : AppCompatActivity() {
    lateinit var binding:ActivityFetViewPager2Binding


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityFetViewPager2Binding.inflate(layoutInflater)
        setContentView(binding.root)
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
            val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
            insets
        }
        val viewPager: ViewPager2 = binding.vp1
        viewPager.adapter = VPGAdapter(this)
        // 底部导航栏目组件
        binding.bottomNavigationView.setOnNavigationItemSelectedListener {item->
            // itemId来自menu
            when (item.itemId) {
                R.id.navigation_home ->viewPager.currentItem = 0
                R.id.navigation_dashboard -> viewPager.currentItem = 1
                R.id.navigation_notifications -> viewPager.currentItem =2
            }
            true
        }

        viewPager.registerOnPageChangeCallback(object: ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                binding.bottomNavigationView.selectedItemId = when (position) {
                    0 -> R.id.navigation_home
                    1 -> R.id.navigation_dashboard
                    else -> R.id.navigation_notifications
                }
            }
        })

    }
}

/*
要使用`ViewPager2`来滑动切换几个底部导航栏的Fragment，你可以按照以下步骤进行：

1. **添加依赖**：确保你的项目中已经添加了`ViewPager2`的依赖。在`build.gradle`文件中添加：
   ```gradle
   implementation "androidx.viewpager2:viewpager2:1.0.0"
   ```

2. **创建布局文件**：在你的布局文件中添加`ViewPager2`和`BottomNavigationView`。例如，在`activity_main.xml`中：
   ```xml
   <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       android:layout_width="match_parent"
       android:layout_height="match_parent">

       <androidx.viewpager2.widget.ViewPager2
           android:id="@+id/viewPager"
           android:layout_width="0dp"
           android:layout_height="0dp"
           app:layout_constraintTop_toTopOf="parent"
           app:layout_constraintBottom_toTopOf="@id/bottomNavigationView"
           app:layout_constraintStart_toStartOf="parent"
           app:layout_constraintEnd_toEndOf="parent"/>

       <com.google.android.material.bottomnavigation.BottomNavigationView
           android:id="@+id/bottomNavigationView"
           android:layout_width="0dp"
           android:layout_height="wrap_content"
           app:layout_constraintBottom_toBottomOf="parent"
           app:layout_constraintStart_toStartOf="parent"
           app:layout_constraintEnd_toEndOf="parent"
           app:menu="@menu/bottom_nav_menu"/>
   </androidx.constraintlayout.widget.ConstraintLayout>
   ```

3. **创建Fragment**：为每个底部导航项创建一个Fragment。例如，创建`HomeFragment`、`SettingsFragment`等。

4. **创建FragmentStateAdapter**：创建一个继承自`FragmentStateAdapter`的类，用于提供ViewPager2的页面。
   ```kotlin
   class ViewPagerAdapter(fragmentActivity: FragmentActivity) : FragmentStateAdapter(fragmentActivity) {
       override fun getItemCount(): Int = 3 // 假设有3个Fragment

       override fun createFragment(position: Int): Fragment {
           return when (position) {
               0 -> HomeFragment()
               1 -> SettingsFragment()
               else -> ThirdFragment()
           }
       }
   }
   ```

5. **初始化ViewPager2**：在你的Activity中，初始化ViewPager2并设置适配器。
   ```kotlin
   class MainActivity : AppCompatActivity() {
       override fun onCreate(savedInstanceState: Bundle?) {
           super.onCreate(savedInstanceState)
           setContentView(R.layout.activity_main)

           val viewPager: ViewPager2 = findViewById(R.id.viewPager)
           viewPager.adapter = ViewPagerAdapter(this)

           val bottomNavigationView: BottomNavigationView = findViewById(R.id.bottomNavigationView)
           bottomNavigationView.setOnNavigationItemSelectedListener { item ->
               when (item.itemId) {
                   R.id.nav_home -> viewPager.currentItem = 0
                   R.id.nav_settings -> viewPager.currentItem = 1
                   R.id.nav_third -> viewPager.currentItem = 2
               }
               true
           }

           viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
               override fun onPageSelected(position: Int) {
                   bottomNavigationView.selectedItemId = when (position) {
                       0 -> R.id.nav_home
                       1 -> R.id.nav_settings
                       else -> R.id.nav_third
                   }
               }
           })
       }
   }
   ```

6. **创建菜单资源文件**：创建一个菜单资源文件`bottom_nav_menu.xml`，定义底部导航栏的菜单项。
   ```xml
   <menu xmlns:android="http://schemas.android.com/apk/res/android">
       <item android:id="@+id/nav_home" android:icon="@drawable/ic_home" android:title="Home"/>
       <item android:id="@+id/nav_settings" android:icon="@drawable/ic_settings" android:title="Settings"/>
       <item android:id="@+id/nav_third" android:icon="@drawable/ic_third" android:title="Third"/>
   </menu>
   ```

这样，你就可以使用`ViewPager2`和`BottomNavigationView`实现滑动切换Fragment的功能了。当用户点击底部导航栏的不同项时，`ViewPager2`会切换到相应的Fragment页面，同时`ViewPager2`的滑动也会更新底部导航栏的选中状态。


 */